(function(){
    // Declare app level module which depends on views, and components
    var app = angular.module('AppCheck.App', [
        'ui.router',
        'restangular',
        'AppCheck.Dashboard',
        'AppCheck.view2',
        'AppCheck.TestManage',
        'AppCheck.UploadApk',
        "services.PartialsService"
    ]);

    app.config(["$stateProvider", "$urlRouterProvider",function ($stateProvider, $urlRouterProvider){

        $urlRouterProvider.otherwise("/testing-dashboard");

        $stateProvider.state('testingDashboard', {
            url: "^/testing-dashboard",
            templateUrl: "/app/dashboard/dashboard.html",
            controller: "dashboardCtrl",
            data: {
                displayName: "DASHBOARD"
            }
        });

        $stateProvider.state('testingDashboard.testOverview', {
            url: "^/test-overview/:testId",
            templateUrl: "/first_test/index.html",
            controller: "dashboardCtrl"
        });

        $stateProvider.state('addTest', {
            url: "^/add-test",
            templateUrl: "/app/test_manage/test_add.html",
            controller: "addTestCtrl",
            data: {
                displayName: "Add New Test"
            }
        });

        $stateProvider.state('uploadApk', {
            url: "^/upload-apk",
            templateUrl: "/app/upload_apk/upload_apk.html",
            controller: "uploadApkCtrl",
            data: {
                displayName: "Upload APK"
            }
        });

    }]);

    app.config(["RestangularProvider", function(RestangularProvider){
        RestangularProvider.setBaseUrl("/api");
        RestangularProvider.setDefaultHttpFields({
            timeout: 20000
        });
        RestangularProvider.setDefaultHeaders({
            'Content-Type': 'application/json'
        });
    }]);

    app.config(function($provide) {
        return $provide.decorator("$templateCache", [
            "$delegate", "$http", "$injector", "PartialsService", "$q",
            function($delegate, $http, $injector, partialsService, $q) {
                var origGetMethod;
                origGetMethod = $delegate.get;

                $delegate.get = function(url) {
                    var cachedResult, partialsPromise;
                    cachedResult = origGetMethod(url);
                    if (cachedResult != null) {
                        return cachedResult;
                    } else {
                        if (/^\/app\/templates\//.test(url)) {
                            partialsPromise = partialsService.getPartial(url).then((function(response) {
                                return response;
                            }), (function(error) {
                                console.warn("Could not load partial '" + url + ": " + error);
                                return $q.reject(error);
                            }));
                            return partialsPromise.then((function(response) {
                                return response;
                            }), (function(error) {
                                return $q.reject(error);
                            }));
                        } else {
                            return origGetMethod(url);
                        }
                    }
                };
                return $delegate;
            }
        ]);
    });
    // app.config(['$routeProvider', function($routeProvider) {
    //    $routeProvider.otherwise({redirectTo: '/dashboard'});
    // }]);

})(this);

